package com.helsinki.mapper;

import com.helsinki.pojo.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface PermissionMapper {

    //查询所有权限
    @Select("select * from Permission")
    List<Permission> findAll();

    @Select("select * from permission pe,role_permission rp where pe.id = rp.permissionId and rp.roleId in" +
            "(select roleId from users_role where userId = #{id})")
    List<Permission> findPermissionByUid(String id);

    @Insert("insert into permission (id,permissionName, url) " +
            "values(#{id},#{permissionName},#{url})")
    void addPermission(Permission permission);

    @Update("update permission set " +
            "permissionName = #{permissionName}," +
            "url = #{url} " +
            "where id = #{id}")
    void updateById(Permission permission);

    @Select("select * from permission where id = #{id}")
    Permission queryById(String id);

    //根据角色查权限
    @Select("select * from permission where id in " +
            "(select permissionId from role_permission where roleId = #{rid})")
    List<Permission> findByRid(String rid);

    @Select("select roleId from role_permission where permissionId = #{pid}")
    List<String> findRoleByPId(String pid);

    @Delete("delete from permission where Id = #{id}")
    void deleteById(String id);

    //设置外键约束
    @Update("SET foreign_key_checks = 0")
    void closeForeignKey();

    @Update("SET foreign_key_checks = 1")
    void openForeignKey();



}
